// /*
//  * @Author: WanYiMing
//  * @Date:2024-05-31-14:19
//  * @Description:
//  */

using Code.Core.CoreBaseClass;
using Code.GameLogic.Common.Event;
using QFramework;
using Unity.VisualScripting;
using UnityEngine;

namespace Code.GameLogic.XianJian.UI
{
    public class XianJianMouseIconUI:BaseController
    {
        private SpriteRenderer _spriteRenderer;
        private XianJianMouseIconVo _XianJianMouseIconVo;
        private GameObject _XianJianSpace;
        private SpriteRenderer _XianJianSpaceSpriteRenderer;
        
        public override IArchitecture GetArchitecture()
        {
            return MainApp.Interface;
        }

        private void Start()
        {
            _spriteRenderer= this.AddComponent<SpriteRenderer>();
            _spriteRenderer.sprite= Resources.Load<Sprite>("地图/建筑物/建筑基座");
            _spriteRenderer.sortingOrder = 4;
            Color tempColor = _spriteRenderer.color;
            tempColor.a = 0.5f;
            _spriteRenderer.color = tempColor;

            _XianJianSpace = new GameObject("范围指示器");
            _XianJianSpace.transform.parent = transform;
            _XianJianSpaceSpriteRenderer = _XianJianSpace.AddComponent<SpriteRenderer>();
            _XianJianSpaceSpriteRenderer.sprite= Resources.Load<Sprite>("地图/建筑物/范围指示器");
            _XianJianSpaceSpriteRenderer.sortingOrder = 3;
            Color tempColor1 = _XianJianSpaceSpriteRenderer.color;
            tempColor1.a = 0.5f;
            _XianJianSpaceSpriteRenderer.color = tempColor1;
            
            _XianJianMouseIconVo = new XianJianMouseIconVo();
            _XianJianMouseIconVo.IsShow = false;
            this.RegisterEvent<MouseShowEs>(MouseShow);
        }


        void Update()
        {
            if (_XianJianMouseIconVo.IsShow)
            {
                MouseMove();
            }
        }


        private void MouseShow(MouseShowEs mouseShowEs)
        {
            _XianJianMouseIconVo.IsShow = mouseShowEs.IsShow;
            if (_XianJianMouseIconVo.IsShow)
            {
                Color tempColor = _spriteRenderer.color;
                tempColor.a = 0.5f;
                _spriteRenderer.color = tempColor;
                _XianJianSpace.transform.localScale = new Vector2(mouseShowEs.Space+1, mouseShowEs.Space+1);
            }
            else
            {
                Color tempColor = _spriteRenderer.color;
                tempColor.a = 0f;
                _spriteRenderer.color = tempColor;
                this.transform.position = new Vector3(-1000, -1000);
            }
        }

        private void MouseMove()
        {
            // 物体跟随鼠标移动
            Vector3 pos = Camera.main.WorldToScreenPoint(transform.position); //将对象坐标换成屏幕坐标
            Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, pos.z); //让鼠标的屏幕坐标与对象坐标一致
            transform.position = Camera.main.ScreenToWorldPoint(mousePos); //将正确的鼠标屏幕坐标换成世界坐标交给物体
        }
    }
}